<?php
namespace excel_query\c;
use jjphp\lib\c;
use PhpOffice\PhpSpreadsheet\IOFactory;


class index extends c{
    const data = [
        //20年两位数01发生月001校代码03年级12班级
        '20010010311'=>[
            'title'=>'三年级11班'
        ],
        '20010010312'=>[
            'title'=>'三年级12班'
        ],
        '20010010313'=>[
            'title'=>'三年级13班'
        ],
        '20010010314'=>[
            'title'=>'三年级14班'
        ],
    ];
    private $class = '20010010314';

    function __construct()
    {
        isset($_GET['class']) && $this->class = $_GET['class'];
        if(!isset(static::data[$this->class])){
            echo '<div style="margin: 0 auto">不存在的数据</div>';
            exit;
        }
        $this->title = static::data[$this->class]['title'];
    }

    function index(){
        include dirname(__DIR__).'/v/index/index.php';
	}

	function query(){
        $error = '';
        $id = intval($_REQUEST['id']??0);
        $name = self::filter_space($_POST['name']??'姓名');
        $reader = IOFactory::createReader('Xlsx');
        $spreadsheet = $reader->load('./excel_query/data/'.$this->class.'.xlsx');
        $sheet = $spreadsheet->getActiveSheet();
        $data= $sheet->toArray();
        $rs[] = $data[0];
        if(isset($data[$id])){
            $tmp = self::filter_space($data[$id][1]);
            if($name === $tmp){
                $rs[] = $data[$id];
            }else{
                $error = '学号或姓名错误';
            }
        }else{
            $error = '学号错误';
        }
        include dirname(__DIR__).'/v/index/query.php';
    }

    private static function filter_space($v){
        return preg_replace('# #','',$v);
    }
}
